feat(applocales): add get_locale_string_from_xaml helper#3371
Open
Wurschdhaud wants to merge 6 commits into
Open
feat(applocales): add get_locale_string_from_xaml helper#3371Wurschdhaud wants to merge 6 commits into
Wurschdhaud wants to merge 6 commits into
Conversation
Resolves locale strings from XAML ResourceDictionary files without requiring a WPF form instance. Tries all locale codes of the current locale, falls back to English, then returns the key as a final fallback. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…es helper Removes load_resource_string and get_translated_ui_string functions. ask_for_preflight_checks now calls applocales.get_locale_string_from_xaml directly. PreflightSelectFromList._setup (merge_resource_dict for WPF template binding) is unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ain__ Replaces three WPFWindow temp_form constructions (used only to call get_locale_string before the real form opened) with a _t() closure backed by applocales.get_locale_string_from_xaml. Removes the raise-Exception control-flow trick; inner try-except eliminated. Outer except now logs with logger.exception then alerts directly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The key is interpolated directly into an XPath attribute predicate surrounded by single quotes. A key containing ' raises xml.etree.ElementTree.ParseError, which is silently swallowed by the blanket except Exception: pass, causing the lookup to silently return the raw key. The fix is to use element iteration with Clark-notation attribute comparison instead of XPath string predicates
…s helper
- Delete check_translations.py and its sys.path manipulation, DocstringMeta
metaclass, and hardcoded cross-bundle path to the pushbutton folder
- Add checks/locale/Checks.ResourceDictionary.{en_us,de_de,fr_fr,es_es,
pt_br,ru,chinese_s}.xaml holding all check-specific strings; pushbutton
ResourceDictionary files now carry only the four UI strings they own
(AuthorLabel, ExtensionLabel, SelectPreflightCheckTitle, RunCheckButton)
- Each check module defines a module-level _t() helper that calls
applocales.get_locale_string_from_xaml with a locale/ relative path,
replacing all get_check_translation() call sites
- Replace DocstringMeta pattern with plain class attributes:
name = _t(...) and ModelChecker.__doc__ = _t(...) — same evaluation
timing, no metaclass magic
- Fix extensions.components.get_checks() to filter .py files only,
preventing imp.load_source from being called on XAML assets in checks/
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends pyRevit’s localization utilities by adding a reusable XAML ResourceDictionary string lookup helper, then refactors existing tools (Preflight Checks + several check scripts + Section Box Navigator launch-time alerts) to use the shared helper instead of bespoke XML parsing / temporary WPF window instantiation. It also hardens extension check discovery to avoid treating non-Python assets as check scripts.
Changes:
- Add
applocales.get_locale_string_from_xaml(xaml_base_path, key)to resolve localized strings from*.ResourceDictionary.{locale}.xamlwith fallback behavior. - Refactor Preflight Checks and multiple
extensions/pyRevitTools.extension/checks/*.pymodules to read translations from dedicatedchecks/locale/Checks.ResourceDictionary.*.xamlfiles via the new helper; removecheck_translations.py. - Update extension check discovery to only return
.pyfiles, avoiding XAML asset compilation attempts.
Reviewed changes
Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pyrevitlib/pyrevit/extensions/components.py | Filter get_checks() results to .py files only to avoid treating XAML/assets as check scripts. |
| pyrevitlib/pyrevit/coreutils/applocales.py | Add get_locale_string_from_xaml helper for localized XAML ResourceDictionary string lookup. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/script.py | Replace ad-hoc XML parsing translation helpers with applocales.get_locale_string_from_xaml. |
| extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Section Box Navigator.pushbutton/script.py | Replace temp-form construction used only for translation lookup with a lightweight _t() closure. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.en_us.xaml | Trim pushbutton-owned ResourceDictionary to only selection dialog strings. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.de_de.xaml | Same trimming for German resources. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.es_es.xaml | Same trimming for Spanish resources. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.fr_fr.xaml | Same trimming for French resources. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.pt_br.xaml | Same trimming for Brazilian Portuguese resources. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.ru.xaml | Same trimming for Russian resources. |
| extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.chinese_s.xaml | Same trimming for Simplified Chinese resources. |
| extensions/pyRevitTools.extension/checks/check_translations.py | Remove legacy translation module that used cross-bundle path assumptions and a metaclass for docstrings/names. |
| extensions/pyRevitTools.extension/checks/grids_check.py | Switch UI strings to _t() backed by new Checks locale dictionaries; simplify class name/__doc__ assignment. |
| extensions/pyRevitTools.extension/checks/levels_check.py | Same translation refactor and minor function/label cleanup. |
| extensions/pyRevitTools.extension/checks/refplanes_check.py | Same translation refactor and class metadata simplification. |
| extensions/pyRevitTools.extension/checks/schedules_not_on_sheet_check.py | Same translation refactor and minor formatting cleanup. |
| extensions/pyRevitTools.extension/checks/worksets_content_check.py | Same translation refactor; remove local path hack and metaclass usage. |
| extensions/pyRevitTools.extension/checks/walltypes_naming_convention_check.py | Same translation refactor; remove local path hack and metaclass usage. |
| extensions/pyRevitTools.extension/checks/modelchecker_Warnings_check.py | Same translation refactor; remove metaclass usage and minor formatting cleanup. |
| extensions/pyRevitTools.extension/checks/cad_audit_check.py | Same translation refactor; replace translation calls with _t() and clean up formatting. |
| extensions/pyRevitTools.extension/checks/audit_all_check.py | Same translation refactor; replace translation calls with _t() and remove metaclass usage. |
| extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.en_us.xaml | Add new check-specific localization dictionary (English). |
| extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.de_de.xaml | Add new check-specific localization dictionary (German). |
| extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.es_es.xaml | Add new check-specific localization dictionary (Spanish). |
| extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.pt_br.xaml | Add new check-specific localization dictionary (Brazilian Portuguese). |
| extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.chinese_s.xaml | Add new check-specific localization dictionary (Simplified Chinese). |
Comment on lines
+295
to
+299
| result = _read_key(path) | ||
| if result is not None: | ||
| return result | ||
| break # right locale file found but key missing — fall to English | ||
|
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
get_locale_string_from_xaml(xaml_base_path, key)toapplocales.py—resolves a string from a
*.ResourceDictionary.{locale}.xamlfile with thesame locale-then-English-then-key fallback as
get_locale_stringload_resource_string/get_translated_ui_stringfunctions from Preflight Checks and replaces them with the new helper
WPFWindowtemp-form constructions in Section Box Navigator__main__(used only to callget_locale_stringbefore the real form opens)with a lightweight
_t()closure; tightens the surrounding error handlingEDIT:
refactor(preflight-checks): replace check_translations with applocales helper
Checklist
Before submitting your pull request, ensure the following requirements are met:
pipenv run black {source_file_or_directory}